
#include "shading.fx"
#include "samplers.fx"
#include "fog.fx"

struct VS_IN
{
	float3 position : POSITION;
	float3 normal	: NORMAL;
	float3 tangent : TANGENT;
	float2 tex		: TEX_COORDS;
};

struct VS_OUT
{
	float4 position : SV_POSITION;
	float4 ws_pos	: WS_POSITION;
	float3 normal	: NORMAL;
	float2 tex		: TEX_COORDS;
};


float4x4 m_World;
float4x4 m_WorldViewProj;



VS_OUT VS(VS_IN vin)
{
	VS_OUT vout;

	vout.position = mul(float4(vin.position, 1.0f), m_WorldViewProj);
	vout.ws_pos = mul(float4(vin.position, 1.0f), m_World);
	vout.normal = mul(float4(vin.normal, 0.0f), m_World).xyz;
	vout.tex =	  vin.tex;
	vout.tex.y += 0.2f;
	return vout;
}




float4 PS(VS_OUT pin) : SV_TARGET
{
	
	// color = shade() : diffuse+specular
	float4 color = float4(shade(pin.ws_pos.xyz, normalize(pin.normal), 0), 1.0f);
			
	
	float4 ambient = float4(0.1f, 0.1f, 0.1f, 0.0f);

	// color	= color	   * tex_diffuse
	// ambient	= ambient  * tex_diffuse
	if (mat_params.vIsTextured.x)
	{
		float4 tex_diff = tex_diffuse.Sample(s_linear, pin.tex);
		color *= tex_diff;
		ambient *= tex_diff;
	}

	// set alpha in w
	color = float4(color.xyz, mat_params.diffuse.w);

	color = computeFog(color, pin.ws_pos.xyz, pin.ws_pos.y, 3.0f, pEye);

	return color;

}




float4 PS_Fog(VS_OUT pin) : SV_TARGET
{

	float4 color = float4(0.1f, 0.1f, 0.1f, mat_params.diffuse.w);


	if (mat_params.vIsTextured.x)
	{
		float4 tex_diff = tex_diffuse.Sample(s_linear, pin.tex);
		color = tex_diff;
	}

	color = float4(color.xyz, mat_params.diffuse.w);

	// 
	color = float4(0.1f, 0.1f, 0.1f, 0.9f);
	float3 fog_pos = pin.ws_pos.xyz;
	color = computeFog(color, fog_pos, pin.ws_pos.y, 3.0f, pEye);

	return color;

}



float4 PS_Fog2(VS_OUT pin) : SV_Target
{
	float3 color = (float3)0;

	if (mat_params.vIsTextured.x)
		color = tex_diffuse.Sample(s_linear, pin.tex).xyz;
		

	return float4(color, 0.1f);

	
}




RasterizerState rs_cull_back
{
	CullMode = Back;
};


RasterizerState rs_cull_front
{
	CullMode = Front;
};


BlendState bs_transparent
{
	AlphaToCoverageEnable = false;
	BlendEnable[0] = true;
	

	SrcBlend[0] = SRC_ALPHA;
	DestBlend[0] = INV_SRC_ALPHA;
	BlendOp[0] = ADD;

	SrcBlendAlpha[0] = ONE;
	DestBlendAlpha[0] = ZERO;
	BlendOpAlpha[0] = ADD; 
};

technique10 Globe_CullBack
{
    pass P0 
	{
        SetVertexShader( CompileShader( vs_4_0, VS() ) );
		SetGeometryShader( NULL);
        SetPixelShader( CompileShader( ps_4_0, PS_Fog2() ) );
		
		SetRasterizerState(rs_cull_back);
		SetBlendState(bs_transparent, float4(0.0f, 0.0f, 0.0f, 0.0f), 0xffffffff);
	}
}

technique10 Globe_CullFront
{
    pass P0 
	{
        SetVertexShader( CompileShader( vs_4_0, VS() ) );
		SetGeometryShader( NULL);
        SetPixelShader( CompileShader( ps_4_0, PS_Fog() ) );
		
		SetRasterizerState(rs_cull_front);
		SetBlendState(bs_transparent, float4(0.0f, 0.0f, 0.0f, 0.0f), 0xffffffff);
	}
}


technique10 Globe_Fog
{
    pass P0 
	{
        SetVertexShader( CompileShader( vs_4_0, VS() ) );
		SetGeometryShader( NULL);
        SetPixelShader( CompileShader( ps_4_0, PS_Fog() ) );
		
		SetRasterizerState(rs_cull_front);
		SetBlendState(bs_transparent, float4(0.0f, 0.0f, 0.0f, 0.0f), 0xffffffff);
	}
}

